﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ForrajeriaBCL;
using System.Data;

namespace ForrajeriaFRONT.Reportes
{
    public partial class VentasDelDia : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)

            {
                calendar.SelectedDate = DateTime.Today;

                FacturaBCL fact = new FacturaBCL();
                DataTable dt = new DataTable();

                try
                {
                    dt = fact.reporteVentasDelDia(calendar.SelectedDate);
                    gridProveedor.DataSource = dt;
                    gridProveedor.DataBind();
                    ViewState["lista"] = dt;
                    ViewState["columna"] = "";
                    ViewState["dire"] = "";

                    if (dt.Rows.Count == 0)
                        lblmensaje.Visible = true;
                }
                catch
                {
                    Response.Redirect("/Default.aspx");
                }

                
            
            }
        }

        protected void cargarGrilla()
        {

            FacturaBCL fac = new FacturaBCL();
            DataTable dt = fac.reporteVentasDelDia(calendar.SelectedDate,ViewState["columna"].ToString(),ViewState["dire"].ToString());

            if (dt.Rows.Count == 0)
                lblmensaje.Visible = true;
            else
                lblmensaje.Visible = false;
            
            gridProveedor.DataSource = dt;
            gridProveedor.DataBind();
            ViewState["lista"] = dt;
            gridDetalle.DataSource = null;
            gridDetalle.DataBind();
            gridProveedor.SelectedIndex = -1;
        }


        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            try
            {
                cargarGrilla();
            }
            catch (Exception ex)
            {
                Response.Redirect("/Default.aspx");
            }
        }


        protected void gridProveedor_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["columna"] = e.SortExpression;

            if (ViewState["dire"].ToString() == "asc")
            {
                ViewState["dire"] = "desc";
            }
            else
            {
                ViewState["dire"] = "asc";
            }

            cargarGrilla();
        }
           
        protected void gridProveedor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {            
            gridProveedor.DataSource = (DataTable)ViewState["lista"];
            gridProveedor.PageIndex = e.NewPageIndex;
            gridProveedor.DataBind();
            
        }

        protected void gridProveedor_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gridProveedor_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            PedidoClienteBCL pedBCL = new PedidoClienteBCL();
            DataTable dtDetalle = new DataTable();

            try
            {
                int nro = int.Parse(gridProveedor.Rows[e.NewSelectedIndex].Cells[1].Text);
                dtDetalle = pedBCL.DetalleReporte(nro);
                gridDetalle.DataSource = dtDetalle;
                gridDetalle.DataBind();

            }
            catch
            {
            }
        }
    }
}